import entryRegistrationApi from '../../../common/api/breed/addentryrgst.api.js'

let _vm
(function (window, document, $) {
    $(function () {
        _vm = new Vue({
            el: "#addentryrgst-form",
            data() {
                return {
                    "isStaff": false,
                    "isInt": false,
                    "name": "",
                    "numCode": "",
                    "phone": "",
                    "inTime": moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                    "carNo": "",
                    "info": "",
                    "reasson": "",
                    "remark": ""
                }
            }
        });
        $('body').attr('data-weui-theme', 'light'); //强制光明模式
        clickFun()
        // _initCommand()
    });
    // 初始化数据
    const _initCommand = async () => {
        // 选择日期
        $('#search-date').on('click', function () {
            // pick();
            isWhichTime()
        });
    }
    // 时间选择器
    const pick = () => {
        weui.datePicker({
            defaultValue: (moment(new Date()).format('YYYY-MM-DD')).split('-'),
            end: moment(new Date()).format('YYYY-MM-DD'),
            className: 'mypick',
            onConfirm: async function (result) {
                let value = `${result[0].value}-${result[1].value}-${result[2].value}`;
                _vm.inTime = moment(new Date(value)).format('YYYY-MM-DD')
            }
        });
    }

    function isWhichTime() {
        let years = [];
        let mouth = [];
        let date = [];
        let hours = [];
        let minite = [];
        if (!years.length && !mouth.length) {
            for (let i = 1889; i <= 2030; i++) {
                years.push({
                    label: i + "年",
                    value: i
                });
            }
        }
        if (!mouth.length) {
            mouth = costomDatePicker(mouth, 1, 13, "月")
        }
        if (!date.length) {
            date = costomDatePicker(date, 1, 32, "日")
        }
        if (!hours.length) {
            hours = costomDatePicker(hours, 0, 24, "时")
        }
        if (!minite.length) {
            minite = costomDatePicker(minite, 0, 60, "分")
        }

        weui.picker(years, mouth, date, hours, minite, {
            //end:2030,
            id: new Date(),//让每一个点击弹框得都有一个不一样得id，这样就避免了共用一个弹框得时候，打开得日期是上一次另外一个得日期回显
            defaultValue: [new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate(), new Date().getHours(), new Date().getMinutes(), new Date().getSeconds()],
            onConfirm: result => {
                //result 是数组，返回得是当前选中得数，如[{lable:'2019年,value:2019},{lable:'2月,value:2}，{lable:'2号,value:2}]
                if (result.length) {
                    //yyyy-mm-dd hh:mm:ss
                    let time = result[0].value + '-' + result[1].value + '-' + result[2].value + ' ' + result[3].value + ':' + result[4].value;
                    console.log('time', time)
                    _vm.inTime = time
                }
            }
        });
    }

    //自定义日期:月时分秒
    function costomDatePicker(years, startTime, endTime, str) {
        for (let j = startTime; j < endTime; j++) {
            years.push({
                label: ('' + j).length === 1 ? '0' + j + str : '' + j + str,
                value: ('' + j).length === 1 ? '0' + j : '' + j,
            });
        }
        return years;
    };

    // 操作函数
    const clickFun = () => {
        $('#isStaff').on('change', function () {
            _vm.isStaff == true ? $('#numCodeAdd').css('display', 'block') : $('#numCodeAdd').css('display', 'none')
            if (_vm.isStaff == false) {
                _vm.numCode = ''
            }
        })
        //取消按钮，返回列表
        $(document).on('click', '#returnMain', function () {
            history.go(-1);
        });

        //取消，关闭弹框
        $(document).on('click', '#returnAddSaleOrders', function () {
            $('#pop').css('display', 'none');
            $('.show-box').css('display', 'none');
            $('.show-box').css({
                'bottom': '-6rem',
                'transition': 'all .ls ease-in'
            });
        });

        // 提交数据
        $('#informationSubmit').on("click", function (e) {
            if (!Utils.IsNotNull(_vm.name)) {
                return $.toptip(`请输入姓名!`, 'warning');
            }
            if ($('#numCodeAdd').css('display') == 'block' && !Utils.IsNotNull(_vm.numCode)) {
                return $.toptip(`请输入工号!`, 'warning');
            }
            if (!Utils.IsNotNull(_vm.phone)) {
                return $.toptip(`请输入电话号码!`, 'warning');
            }
            if (!Utils.IsNotNull(_vm.inTime)) {
                return $.toptip(`请输入到厂时间!`, 'warning');
            }
            if (!Utils.IsNotNull(_vm.info)) {
                return $.toptip(`请输入7日风险信息!`, 'warning');
            }
            if (!Utils.IsNotNull(_vm.reasson)) {
                return $.toptip(`请输入到厂原因!`, 'warning');
            }

            // 电话号码
            const PHONE_VERIFICATION = /^1[3456789]\d{9}$/;
            if (!(PHONE_VERIFICATION.test(_vm.phone))) return $.toptip(`请输入正确的电话号码`, 'warning')
            //车牌
            const LICENSE_PLATE = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/;
            if (Utils.IsNotNull(_vm.carNo) && !(LICENSE_PLATE.test(_vm.carNo))) return $.toptip(`请输入正确的车牌号`, 'warning')
            savetips();
        })
    }
    // 保存
    const savetips = () => {
        $.modal({
            autoClose: false,
            title: '提示',
            text: `<div class="weui-cells__group weui-cells__group_form" style = "margin-top:-5px" >
                    <div class="addedientry" style="padding:5px;display:flex;justify-content: center; padding-bottom: 20px;"><div class="addentry">是否确认保存并提交该单据？</div></div></div>`,
            buttons: [
                {
                    text: "取消", className: "default", onClick: function () {
                        $.closeModal();
                    }
                },
                {
                    text: "确定", onClick: function () {
                        $.closeModal();
                        let datas = {
                            "isStaff": _vm.isStaff,
                            "isInt": _vm.isInt,
                            "name": _vm.name,
                            "numCode": _vm.numCode,
                            "phone": _vm.phone,
                            "inTime": _vm.inTime,
                            "carNo": _vm.carNo,
                            "info": _vm.info,
                            "reasson": _vm.reasson,
                            "remark": _vm.remark
                        };
                        console.log(datas)
                        entryRegistrationApi.addEntryRegistration(datas).then(res => {
                            entryRegistrationApi.submitPutEntryRegistration(res).then(res1 => {
                                $.toptip('入厂登记新增成功！', 'success');
                                setTimeout(function () {
                                    location.href = `./entryrgstsubmited.html `;
                                }, 1000);
                            })
                        }, err => {
                            $.toptip(err.responseJSON?.msg, 'error');
                        });
                    }
                }
            ]
        });
    }

})(window, document, jQuery)